package cn.iocoder.yudao.module.energy.service.report;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.energy.controller.admin.report.vo.EnergyReportDataBatchImportVO;
import cn.iocoder.yudao.module.energy.controller.admin.report.vo.EnergyReportDataCreateReqVO;
import cn.iocoder.yudao.module.energy.controller.admin.report.vo.EnergyReportDataPageReqVO;
import cn.iocoder.yudao.module.energy.controller.admin.report.vo.EnergyReportDataUpdateReqVO;
import cn.iocoder.yudao.module.energy.convert.report.EnergyReportDataConvert;
import cn.iocoder.yudao.module.energy.dal.dataobject.report.EnergyReportDataDO;
import cn.iocoder.yudao.module.energy.dal.mysql.report.EnergyReportDataMapper;
import cn.iocoder.yudao.module.energy.enums.report.EnergyReportUploadTypeEnum;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.energy.enums.ErrorCodeConstants.REPORT_DATA_NOT_EXISTS;

/**
 * 能耗报告数据 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class EnergyReportDataServiceImpl implements EnergyReportDataService {

    @Resource
    private EnergyReportDataMapper reportDataMapper;

    @Override
    public Long createReportData(EnergyReportDataCreateReqVO createReqVO) {
        // 插入
        EnergyReportDataDO reportData = EnergyReportDataConvert.INSTANCE.convert(createReqVO);
        
        // 确保 energyConsumption 字段被正确设置
        if (createReqVO.getEnergyConsumption() != null) {
            reportData.setEnergyConsumption(createReqVO.getEnergyConsumption());
        }
        
        // 设置上传类型 - 如果已经设置则使用已有值，否则默认为随机生成上传
        if (createReqVO.getUploadType() == null) {
            reportData.setUploadType(EnergyReportUploadTypeEnum.RANDOM_GENERATE.getType());
        }
        
        reportDataMapper.insert(reportData);
        // 返回
        return reportData.getId();
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int batchCreateReportData(List<EnergyReportDataBatchImportVO> batchImportVOs) {
        if (batchImportVOs == null || batchImportVOs.isEmpty()) {
            return 0;
        }
        
        // 将批量导入VO转换为DO对象列表
        List<EnergyReportDataDO> reportDataList = EnergyReportDataConvert.INSTANCE.convertList(batchImportVOs);
        
        // 确保每个对象的energyConsumption字段和uploadType字段被正确设置
        for (int i = 0; i < batchImportVOs.size(); i++) {
            EnergyReportDataBatchImportVO vo = batchImportVOs.get(i);
            EnergyReportDataDO reportData = reportDataList.get(i);
            
            if (vo.getEnergyConsumption() != null) {
                reportData.setEnergyConsumption(vo.getEnergyConsumption());
            }
            
            // 设置上传类型 - 如果已经设置则使用已有值，否则默认为批量上传
            if (vo.getUploadType() == null) {
                reportData.setUploadType(EnergyReportUploadTypeEnum.BATCH_IMPORT.getType());
            }
        }
        
        // 批量插入
        int insertCount = 0;
        for (EnergyReportDataDO reportData : reportDataList) {
            insertCount += reportDataMapper.insert(reportData);
        }
        
        return insertCount;
    }

    @Override
    public void updateReportData(EnergyReportDataUpdateReqVO updateReqVO) {
        // 校验存在
        validateReportDataExists(updateReqVO.getId());
        
        // 创建更新对象
        EnergyReportDataDO updateObj = EnergyReportDataConvert.INSTANCE.convert(updateReqVO);
        
        // 确保 energyConsumption 字段被正确设置
        if (updateReqVO.getEnergyConsumption() != null) {
            updateObj.setEnergyConsumption(updateReqVO.getEnergyConsumption());
        }
        
        // 直接执行更新
        reportDataMapper.updateById(updateObj);
    }

    @Override
    public void deleteReportData(Long id) {
        // 校验存在
        validateReportDataExists(id);
        // 删除
        reportDataMapper.deleteById(id);
    }

    private void validateReportDataExists(Long id) {
        if (reportDataMapper.selectById(id) == null) {
            throw exception(REPORT_DATA_NOT_EXISTS);
        }
    }

    @Override
    public EnergyReportDataDO getReportData(Long id) {
        return reportDataMapper.selectById(id);
    }

    @Override
    public PageResult<EnergyReportDataDO> getReportDataPage(EnergyReportDataPageReqVO pageReqVO) {
        // 处理只查询uploadType有值的数据
        // 注意：这里使用mapper已有的selectPage方法，不再使用lambda表达式方式
        // 因为EnergyReportDataMapper.selectPage实现里已经处理了uploadTypeExists的条件
        return reportDataMapper.selectPage(pageReqVO);
    }
    
    @Override
    public List<EnergyReportDataDO> getReportDataList(EnergyReportDataPageReqVO pageReqVO) {
        // 直接使用Mapper查询列表，不进行分页
        return reportDataMapper.selectList(pageReqVO);
    }
}